#!coding:utf-8
# vim: tabstop=4 shiftwidth=4 softtabstop=4
from lxml import etree
import logging

from cvirt.cnode.libvirtXmlObject.baseDomain import XmlObject,GuestDevice

LOG = logging.getLogger(__name__)

"""
    @author jay.han
"""

class GuestDiskList(XmlObject):
    "TODO : this methods should move to GuestDevice"
    ROOT_NAME = 'disk'
    def __init__(self, parse_xml=None, **kw):
        super(GuestDiskList, self).__init__(parse_xml=parse_xml,**kw)
        self._xpath = etree.XPath("//%s" % self.ROOT_NAME )

        self.disks = []

    def _parse_xml(self, xmldoc):
        for dom in self._xpath(xmldoc):
            disk = GuestDisk()
            disk._parse_xml(dom)
            self.disks.append(disk)

    def get_type_disk(self,disk_name):
        pass



class GuestDisk(GuestDevice):
    ROOT_NAME = 'disk'
    def __init__(self, parse_xml=None, **kw):
        super(GuestDisk, self).__init__(parse_xml=parse_xml,**kw)

        self.source_type = "file"
        self.source_device = "disk"
        self.driver_name = 'qemu'
        self.driver_format = None
        self.driver_cache = None
        self.driver_io = None
        self.source_path = None
        self.source_protocol = None
        self.source_host = None
        self.target_dev = None
        self.target_path = None
        self.target_bus = None
        self.auth_username = None
        self.auth_secret_type = None
        self.auth_secret_uuid = None
        self.serial = None
        self.disk_read_bytes_sec = None
        self.disk_read_iops_sec = None
        self.disk_write_bytes_sec = None
        self.disk_write_iops_sec = None
        self.disk_total_bytes_sec = None
        self.disk_total_iops_sec = None
        self.readonly = False

    def _parse_xml(self, xmldoc):
        self.source_type = xmldoc.get('type')
        self.source_device = xmldoc.get('device')
        for c in xmldoc.getchildren():
            if c.tag == 'driver':
                self.driver_name = c.get('name')
                self.driver_format = c.get('type')
                self.driver_cache = c.get('cache')
            elif c.tag == 'source':
                self.source_path = c.get('file') if c.get('file') else c.get('dev')
            elif c.tag == 'target':
                self.target_dev = c.get('dev')
                self.target_bus = c.get('bus')
            elif c.tag == 'readonly':
                self.readonly = True

    def _get_xml_config(self, root):
        dev = root

        dev.set("type", self.source_type)
        dev.set("device", self.source_device)
        if (self.driver_name is not None or
                    self.driver_format is not None or
                    self.driver_cache is not None):
            drv = etree.Element("driver")
            if self.driver_name is not None:
                drv.set("name", self.driver_name)
            if self.driver_format is not None:
                drv.set("type", self.driver_format)
            if self.driver_cache is not None:
                drv.set("cache", self.driver_cache)
            if self.driver_io is not None:
                drv.set('io',self.driver_io)
            dev.append(drv)

        if self.source_type == "file" and self.source_path:
            dev.append(etree.Element("source", file=self.source_path))
        elif self.source_type == "block":
            dev.append(etree.Element("source", dev=self.source_path))
        elif self.source_type == "mount":
            dev.append(etree.Element("source", dir=self.source_path))
        elif self.source_type == "network":
            dev.append(etree.Element("source", protocol=self.source_protocol,
                                     name=self.source_host))

        if self.auth_secret_type is not None:
            auth = etree.Element("auth")
            auth.set("username", self.auth_username)
            auth.append(etree.Element("secret", type=self.auth_secret_type,
                                      uuid=self.auth_secret_uuid))
            dev.append(auth)

        if self.source_type == "mount":
            dev.append(etree.Element("target", dir=self.target_path))
        else:
            dev.append(etree.Element("target", dev=self.target_dev,
                                     bus=self.target_bus))

        if self.serial is not None:
            dev.append(self._set_text("serial", self.serial))

        iotune = etree.Element("iotune")

        if self.disk_read_bytes_sec is not None:
            iotune.append(self._set_text("read_bytes_sec",
                                         self.disk_read_bytes_sec))

        if self.disk_read_iops_sec is not None:
            iotune.append(self._set_text("read_iops_sec",
                                         self.disk_read_iops_sec))

        if self.disk_write_bytes_sec is not None:
            iotune.append(self._set_text("write_bytes_sec",
                                         self.disk_write_bytes_sec))

        if self.disk_write_iops_sec is not None:
            iotune.append(self._set_text("write_iops_sec",
                                         self.disk_write_iops_sec))

        if self.disk_total_bytes_sec is not None:
            iotune.append(self._set_text("total_bytes_sec",
                                         self.disk_total_bytes_sec))

        if self.disk_total_iops_sec is not None:
            iotune.append(self._set_text("total_iops_sec",
                                         self.disk_total_iops_sec))

        if len(iotune) > 0:
            dev.append(iotune)

        if self.readonly:
            dev.append(etree.Element("readonly"))

        return dev


class GuestCdrom(GuestDisk):
    def __init__(self, parse_xml=None, **kw):
        super(GuestCdrom, self).__init__(parse_xml=parse_xml,**kw)
        self.source_device = 'cdrom'
        self.driver_name = 'qemu'
        self.driver_format = 'raw'
        self.target_dev = 'hdc'
        self.target_bus='ide'
